<?php
/**
 * Created by PhpStorm.
 * User: lhuan
 * Date: 2019/11/4
 * Time: 22:29
 */
namespace app\index\model;

use think\Model;

class ArticleClassify extends Model
{
    public $map = [];
    public $fields = '*';

    public function initialize()
    {
        parent::initialize();
    }
    public static function getUrl($pid,$id)
    {
        $r = ArticleClassify::field('id,url')->find($pid);
        return (isset($r->url)?$r->url:'/news') . '/'.$id.'.html';
    }

    public static function getName($pid)
    {
        return ArticleClassify::field('id,name')->find($pid)->name;
    }

    public static function getClassify($pid)
    {
        return ArticleClassify::field('*')->find($pid);
    }

    public static function getAllClassify()
    {
        $data = ArticleClassify::where(['pid'=> 0])->order('id asc')->select();
        $data = data_trans($data);
        $r = [];
        foreach($data as $k => $e)
        {
            $r[] = [
                'value' => $e['id'],
                'label'=> $e['name'],
            ];
            $son = ArticleClassify::where(['pid'=> $e['id']])->order('id asc')->select();
            if(!empty($son)){
                $son = data_trans($son);
                foreach($son as $v){
                    $r[$k]['children'][] = [
                        'value' => $v['id'],
                        'label' => $v['name']
                    ];
                }

            }
        }
        return $r;
    }

}